home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 020 / multitasking / example.c next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  145 lines

  1. /*  :ts=8
  2.  * Code to open various resources under Intuition
  3.  */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/io.h>
  7.  
  8. #define REV        0
  9. #define SUB_PORT    "Subtsk Port"
  10. #define    SUB_REPLY    "Subtsk Reply"
  11.  
  12. #define CMD_HELLO    CMD_NONSTD
  13. #define    CMD_SUICIDE    (CMD_NONSTD + 1)
  14.  
  15. struct infopacket    *CreateExtIO();
  16.  
  17. struct infopacket {
  18.     struct Message msg;
  19.     long command;
  20. } *sub_cmd;
  21.  
  22. struct MsgPort        *subport, *subreply;
  23. struct Task        *subtask;
  24. void            neeto();
  25.  
  26.  
  27. openstuff ()
  28. {
  29.     if (!(subreply = CreatePort (SUB_REPLY, 0L)))
  30.         bomb ("I need a reply port.");
  31.  
  32.     /*  We can get away with this  */
  33.     if (!(sub_cmd = CreateExtIO (subreply, (long) sizeof (*sub_cmd))))
  34.         bomb ("Can't make subtsk ExtIO.");
  35.  
  36.     subtask = CreateTask ("Neeto", 1, neeto, 2048L);
  37.     if (!(subport = FindPort (SUB_PORT)))
  38.         bomb ("Can't find SUB_PORT.");
  39. }
  40.  
  41. closestuff ()
  42. {
  43.     if (subtask) {
  44.         if (subport) {
  45.             sub_cmd -> command = CMD_SUICIDE;
  46.             PutMsg (subport, sub_cmd);
  47.             WaitPort (subreply);
  48.             GetMsg (subreply);
  49.         }
  50.         DeleteTask (subtask);
  51.     }
  52.     if (sub_cmd)
  53.         DeleteExtIO (sub_cmd, (long) sizeof (*sub_cmd));
  54.     if (subreply)
  55.         DeletePort (subreply);
  56. }
  57.  
  58. bomb (str)
  59. char *str;
  60. {
  61.     printf ("%s\n", str);
  62.     closestuff ();
  63. }
  64.  
  65.  
  66.  
  67. /*
  68.  * The sub-task.
  69.  */
  70.  
  71. void
  72. neeto ()
  73. {
  74.     struct infopacket *cmd;
  75.     struct MsgPort *prgport = NULL;
  76.  
  77.     /*  Since this is an independent task, we'll have to do
  78.      *  everything ourselves.....
  79.      */
  80.     if (!(prgport = CreatePort (SUB_PORT, 1L)))
  81.         goto die;
  82.  
  83.     /*
  84.      * Stand around twiddling our thumbs until the main program
  85.      * wants us to do something.
  86.      */
  87.     WaitPort (prgport);
  88.     cmd = GetMsg (prgport);
  89.     if (cmd -> command != CMD_HELLO)
  90.         /*
  91.          * If we come here, then something is very wrong.  Let the
  92.          * machine crash (we can't do printf's from a task).
  93.          */
  94.         bomb ("neeto(): Hello???");
  95.     ReplyMsg (cmd);
  96.  
  97.     while (1) {
  98.         WaitPort (prgport);
  99.  
  100.         while (cmd = (struct infopacket *) GetMsg (prgport))
  101.             switch (cmd -> command) {
  102.             case CMD_UPDATE:
  103.                 cmd -> command = 0;
  104.                 ReplyMsg (cmd);
  105.                 break;
  106.             case CMD_SUICIDE:
  107.                 goto die;
  108.             default:
  109.                 ReplyMsg (cmd);
  110.             }
  111.     }
  112. die:
  113.     if (prgport)
  114.         DeletePort (prgport);
  115.     if (cmd)
  116.         /*  This must be the suicide message, so reply it  */
  117.         ReplyMsg (cmd);
  118.  
  119.     /*  Wait for Godot  */
  120.     Wait (0L);
  121. }
  122.  
  123. main ()
  124. {
  125.     int i;
  126.  
  127.     openstuff ();
  128.  
  129.     sub_cmd -> command = CMD_HELLO;
  130.     PutMsg (subport, sub_cmd);
  131.     WaitPort (subreply);
  132.     GetMsg (subreply);
  133.  
  134.     for (i=0; i<10; i++) {
  135.         PutMsg (subport, sub_cmd);
  136.         WaitPort (subreply);
  137.         GetMsg (subreply);
  138.         if (i == 6)
  139.             sub_cmd -> command = CMD_UPDATE;
  140.         printf ("command = %ld\n", sub_cmd -> command);
  141.     }
  142.  
  143.     closestuff ();    /*  This kills off the subtask  */
  144. }
  145.